package com.zyq.controller;

import com.alibaba.fastjson.JSON;
import com.zyq.entity.Person;
import com.zyq.service.PersonService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ZYQ on 2019/1/25.
 */
@Controller
public class PersonController {
    @Resource
    private PersonService personService;
//    @Resource
//    private RedisTemplateUtil redisTemplateUtil;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @RequestMapping(value = "/SelectAll")
    public String SelectAll(HttpServletRequest request){
        String personstr=(String) redisTemplate.opsForList().leftPop("personList1");
        List<Person> personList=null;
        List<Map<String, Object>> listProfit=null;
        if(personstr==null){
            System.out.println("还没有缓存!准备查询。。。。");
            personList=personService.SelectAll();
            redisTemplate.opsForList().leftPush("personList1",JSON.toJSONString(personList));
            request.getSession().setAttribute("personList",personList);
        }else{
            System.out.println("已经缓存啦！");
            listProfit=new ArrayList<Map<String,Object>>();
            List<Map> mapList=JSON.parseArray(personstr, Map.class);
            for (Map map : mapList) {
                Map<String,Object> sObj=(Map<String,Object>)map;
                listProfit.add(sObj);
            }
            request.getSession().setAttribute("personList",listProfit);
        }

        return  "index";
    }
}
